package com.pan.server.controller;

import com.pan.common.constant.JwtClaimsConstant;
import com.pan.common.properties.JwtProperties;
import com.pan.common.result.Result;
import com.pan.common.utils.JwtUtil;
import com.pan.pojo.dto.UserDTO;
import com.pan.pojo.dto.UserLoginDTO;
import com.pan.pojo.entity.User;
import com.pan.pojo.vo.UserVO;
import com.pan.server.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RequestMapping("/common")
@Slf4j
@Api(tags = "公共控制器")
@RestController
@CrossOrigin("http://localhost:7070")
public class CommonController {
    @Autowired
    private JwtProperties jwtProperties;
    @Autowired
    private UserService userService;

    /**
     * 注册
     * @param userDTO
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public Result<UserVO> Register(@RequestBody UserDTO userDTO){
        log.info("注册流程开始... 用户名：{},密码：{}", userDTO.getUsername(), userDTO.getPassword());

        User user = userService.register(userDTO);

        UserVO userVO = UserVO.builder()
                .username(user.getUsername())
                .name(user.getName())
                .gender(user.getGender())
                .location(user.getLocation())
                .permissionId(user.getPermissionId())
                .token(null)
                .build();

        return Result.success(userVO);
    }

    /**
     * 登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result<UserVO> login(@RequestBody UserLoginDTO userLoginDTO){
        log.info("登录流程开始... 用户名：{},密码：{}", userLoginDTO.getUsername(), userLoginDTO.getPassword());

        User user = userService.login(userLoginDTO);

        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getUserId());
        String token = JwtUtil.createJWT(
                jwtProperties.getUserSecretKey(),
                jwtProperties.getUserTtl(),
                claims
        );

        UserVO userVO = UserVO.builder()
                .username(user.getUsername())
                .name(user.getName())
                .gender(user.getGender())
                .location(user.getLocation())
                .permissionId(user.getPermissionId())
                .token(token)
                .build();

        return Result.success(userVO);
    }

    @PostMapping(value = "/logout")
    @ApiOperation(value = "用户登出")
    public Result<String> logout(){
        log.info("用户登出...");
        return Result.success();
    }

    @PostMapping(value = "/updateUser")
    @ApiOperation(value = "更新用户信息")
    public Result<User> updateUser(@RequestBody UserDTO userDTO){
        log.info("更新用户信息...");

        User user = userService.updateUser(userDTO);

        return Result.success(user);
    }
}